OB.Sys_Message_Alert = {
	index: 0,
	runGetMessages: function() {
		window.setTimeout(function() {
			OB.RemoteCallManager.call("com.basis.handler.MessageAlertHandler", {}, {}, 
				function(response, data, request) {
					if (data.success && data.list.length > 0) {
						OB.Sys_Message_Alert.showMessage(data.list);
					} else {
						OB.Sys_Message_Alert.runGetMessages();
					}
				}
			);
		}, 120000);//每2分钟刷新一次消息提醒
	},
	showMessage: function(list) {
		this.msgList = list;
		this.index = 0;
		this.nextMessage();
	},
	nextMessage: function() {
		if (this.index >= this.msgList.length) {
			this.runGetMessages();
			return;
		}
		this.alertMessage(
			this.msgList[this.index].titie,
			this.msgList[this.index].text,
			this.msgList[this.index].url,
			this.msgList[this.index].date
		);
		this.messageId = this.msgList[this.index].id;
		this.index ++;
	},
	alertMessage: function(title, text, url, date) {
		if (!this.Msg_Alert_Window) {
			this.msgInfo = isc.Label.create({
				align: "center",
				padding: 5,
				width: "100%",
				height: "135",
				contents: ""
			});
			this.dateInfo = isc.Label.create({
				padding: 5,
				width: "190",
				height: "20",
				contents: ""
			});
			
			this.okButton = isc.OBFormButton.create({
				title : '确定',
				popup : this,
				action : function() {
					OB.Sys_Message_Alert.nextMessage();
					
					OB.RemoteCallManager.call("com.basis.handler.MessageSetReadHandler", 
						{messageId: OB.Sys_Message_Alert.messageId}, {}, 
						function(response, data, request) {
							if (!data.success) {
								isc.showMessage(data.msg);
							}
						}
					);
					OB.Sys_Message_Alert.Msg_Alert_Window.closeClick();
				}
			});
			
			this.nextButton = isc.OBFormButton.create({
				title : '下一条',
				popup : this,
				action : function() {
					OB.RemoteCallManager.call("com.basis.handler.MessageSetReadHandler", 
						{messageId: OB.Sys_Message_Alert.messageId}, {}, 
						function(response, data, request) {
							if (!data.success) {
								isc.showMessage(data.msg);
							}
						}
					);
					OB.Sys_Message_Alert.nextMessage();
				}
			});
			
			this.Msg_Alert_Window = isc.Window.create({
				id: "Msg_Alert_Window",
				width: 320,
				height: 200,
				title: '消息提醒',
				left: document.body.clientWidth - 302,
				top: document.body.clientHeight - 202,
				canDragResize: false,
				canDragReposition: true,
				showCloseButton: false,
				showMinimizeButton: false,
				items: [
					isc.VLayout.create({
						width: "100%",
						height: "170",
						align: "center",
						members: [
							this.msgInfo,
							isc.HLayout.create({
								width: "100%",
								height: "30",
								align: "right",
								padding: 5,
								members: [
									this.dateInfo,
									this.okButton,
									this.nextButton
								]
							})
						]
					})
				]
			});
		}
		this.Msg_Alert_Window.setTitle(title);
		this.msgInfo.setContents(text);
		this.dateInfo.setContents("消息时间：" + date);
		if (this.index < this.msgList.length - 1) {
			this.okButton.hide();
			this.nextButton.show();
		} else {
			this.okButton.show();
			this.nextButton.hide();
		}
		this.Msg_Alert_Window.show();
	}
};

window.setTimeout(function() {
	OB.Sys_Message_Alert.runGetMessages();
}, 1000);

function alertObject(obj, filter) {
	var array = [];
	for (var key in obj) {
		if (filter) {
			if (key.indexOf(filter) == -1) {
				continue;
			}
		}
		array.push(key + ": " + obj[key]);
	}
	alert(array.join(", \n"));
}

Date.prototype.toString = function(bool) {
	var str = this.getFullYear() + "-" + 
		(this.getMonth() > 8 ? this.getMonth() + 1 : "0" + (this.getMonth() + 1)) + "-" + 
		(this.getDate() > 9 ? this.getDate() : "0" + (this.getDate()));
	if (bool) {
		str += (" " + (this.getHour() > 9 ? this.getHour() : "0" + (this.getHour())) + 
				":" + (this.getMinute() > 9 ? this.getMinute() : "0" + (this.getMinute())) + ":00");
	}
	return str;
}
